var url = require('url');
var fs = require('fs');
var path = require('path');

gulp = require('gulp');
livereload = require('gulp-livereload');
webserver = require('gulp-webserver');
gulp.task('webserver', function() {
    gulp.src('.') // 服务器目录（./代表根目录）
    .pipe(webserver({ // 运行gulp-webserver
        port: 8000, //端口，默认8000
        directoryListing: {
            enable: true,
            path: '.'
        },
        middleware: function(req, res, next) {
            console.log(req)
            //mock local data
            res.setHeader('Access-Control-Allow-Origin','*')
           
            var urlObj = url.parse(req.url, true),
                method = req.method;
                switch (urlObj.pathname) {
                    case '/api/datajson':
                        var data = {
                            "福州市":{
                                "adcode":"350100",
                                "all":[
                                    { "position":[118.17 , 26.65],"content":'仓山区'}]
                            },
                            "龙岩市":{
                                "adcode":"350800",
                                "all":[
                                { "position":[116.30946, 39.937629 ],"content":'连城县'}]
                            },
                            "南平市":{
                                "adcode":"350700",
                                "all":[
                                { "position":[118.17 , 26.65],"content":'建阳区'}]
                            },
                            "宁德市":{
                                "adcode":"350900",
                                "all":[
                                { "position":[116.30946, 39.937629],"content":'古田县'}]
                            },
                            "厦门市":{
                                "adcode":"350200",
                                "all":[
                                { "position":[116.30946, 39.937629],"content":'思明区'}]
                            }
                           };
                        res.setHeader('Content-Type', 'application/json');
                        res.end(JSON.stringify(data));
                        return;
                    case '/api/goods':
                        // ...
                        return;
                    case '/api/images':
                        // ...
                        return;
                    default:
                        ;
                }
            next();
        },
    }));
});


// 默认任务
gulp.task('default', ['webserver']);